package com.bc.bcapi.service;

import com.baidu.disconf.client.utils.StringUtil;
import com.bc.bcapi.dao.T_Project_tag_relationMapper;
import com.bc.bcapi.dao.T_User_joinMapper;
import com.bc.bcmodel.domain.T_Project_tag_relationModel;
import com.bc.bcmodel.domain.T_User_joinModel;
import com.bc.bcmodel.enums.IntegralRecordEnum;
import com.bc.bcmodel.enums.ProjectJoinEnum;
import com.bc.bcmodel.req.ProjectTagReq;
import com.bc.bcmodel.resp.ProjectTagBaseData;
import com.bc.bcmodel.resp.ProjectTagResp;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.bc.commonentity.enums.SeqNameEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_Project_tagMapper;
import com.bc.bcmodel.domain.T_Project_tagModel;

/**
 * @Name T_Project_tagService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_Project_tagService {
    @Resource
    T_Project_tagMapper t_Project_tagMapper;
    @Resource
    T_SequenceService t_sequenceService;
    @Resource
    T_User_joinMapper t_user_joinMapper;
    @Resource
    T_Project_tag_relationMapper t_project_tag_relationMapper;
    @Resource
    T_Integral_recordService t_integral_recordService;

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_Project_tagModel req) {
        return t_Project_tagMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_Project_tagModel> req) {
        return t_Project_tagMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_Project_tagModel findByNamedParam(T_Project_tagModel req) {
        return t_Project_tagMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_Project_tagModel> getAllList(T_Project_tagModel req) {
        return t_Project_tagMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_Project_tagModel> getList(T_Project_tagModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Project_tagModel> list = t_Project_tagMapper.findByNamedParamList(req);
        return new PageInfo<T_Project_tagModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_Project_tagModel req) {
        return t_Project_tagMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_Project_tagMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_Project_tagModel req) {
        return t_Project_tagMapper.count(req);
    }

    /**
     * 查询用户的声音
     */
    public JsonResult getTag(ProjectTagReq req) {
        JsonResult js = new JsonResult();
        ProjectTagResp resp = new ProjectTagResp();
        List<ProjectTagBaseData> list = t_Project_tagMapper.getTag(req.getPid());
        //显示我评论
        int is_show_comment = 1;
        if (!StringUtils.isEmpty(req.getUser_id())) {

            //查询当前用户当前项目所有声音
            T_Project_tag_relationModel t_project_tag_relationModel = new T_Project_tag_relationModel();
            t_project_tag_relationModel.setUser_id(req.getUser_id());
            t_project_tag_relationModel.setPid(req.getPid());
            List<T_Project_tag_relationModel> listRelationModel =
                    t_project_tag_relationMapper.findByNamedParamList(t_project_tag_relationModel);

            int counter = getUserTagCounter(req.getPid(), req.getUser_id());
            if (counter >= 3) {
                //不显示评论
                is_show_comment = 0;
            }
            //设置我的声音
            for (ProjectTagBaseData item :
                    list) {
                String tid = item.getTid();
                if (listRelationModel.stream().filter(t -> t.getTid().equals(tid)).count() > 0) {
                    item.setIs_mytag(1);
                }
            }
        }
        resp.setProjectTagBaseDataList(list);
        resp.setIs_show_comment(is_show_comment);
        js.setData(resp);
        return js;
    }

    /**
     * 查询用户添加声音次数
     */
    private int getUserTagCounter(String pid, String user_id) {
        T_Project_tagModel t_project_tagModelCounter = new T_Project_tagModel();
        t_project_tagModelCounter.setPid(pid);
        t_project_tagModelCounter.setUser_id(user_id);
        int counter = t_Project_tagMapper.count(t_project_tagModelCounter);
        return counter;
    }

    private int getUserTagByName(String pid, String tag_name) {
        T_Project_tagModel t_project_tagModelCounter = new T_Project_tagModel();
        t_project_tagModelCounter.setPid(pid);
        t_project_tagModelCounter.setTag_name(tag_name);
        int counter = t_Project_tagMapper.count(t_project_tagModelCounter);
        return counter;
    }

    /**
     * 添加用户的声音
     */
    public JsonResult setTag(T_Project_tagModel req) {
        JsonResult js = new JsonResult();
        if (StringUtils.isEmpty(req.getTag_name()) ||
                StringUtils.isEmpty(req.getPid())) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        ProjectTagResp resp = new ProjectTagResp();
        //查询文字长度不能大于
        if (req.getTag_name().length() > 40) {
            return new JsonResult(HttpReturnRnums.PROJECT_TAG_LENGTH_ERROR);
        }
        //查询当前用户是否发布大于三次，
        int counter = getUserTagCounter(req.getPid(), req.getUser_id());
        if (counter >= 3) {
            return new JsonResult(HttpReturnRnums.PROJECT_TAG_COUNTER_ERROR);
        }
        int nameCounter = getUserTagByName(req.getPid(), req.getTag_name());
        if (nameCounter > 0) {
            //不可与已有标签重复
            return new JsonResult(HttpReturnRnums.PROJECT_TAG_COUNTER_EXISTS);
        }
        String tid = t_sequenceService.genSequence(SeqNameEnum.PROJECT_TAG_ID);
        req.setCounter(0);
        req.setTid(tid);
        t_Project_tagMapper.insert(req);
        int Is_show_comment = 1;
        if ((counter + 1) >= 3) {
            Is_show_comment = 0;
        }
        resp.setIs_show_comment(Is_show_comment);
        T_User_joinModel t_user_joinModel = new T_User_joinModel();
        t_user_joinModel.setPid(req.getPid());
        t_user_joinModel.setUser_id(req.getUser_id());
        t_user_joinModel.setType_id(ProjectJoinEnum.DB.value());
        t_user_joinMapper.insert(t_user_joinModel);
        resp.setTid(tid);
        js.setData(resp);
        //添加打标积分
        t_integral_recordService.threadInsertIntegralRecord(
                IntegralRecordEnum.DBICO, req.getPid(), req.getUser_id());
        return js;
    }

    /**
     * 支持某个用户的声音
     */
    public JsonResult goodTag(T_Project_tagModel req) {
        if (StringUtils.isEmpty(req.getPid()) || StringUtils.isEmpty(req.getTid())) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        JsonResult js = new JsonResult();
        //判断是否有点赞过
        T_Project_tag_relationModel t_project_tag_relationModel =
                new T_Project_tag_relationModel();
        t_project_tag_relationModel.setTid(req.getTid());
        t_project_tag_relationModel.setPid(req.getPid());
        t_project_tag_relationModel.setUser_id(req.getUser_id());

        T_User_joinModel t_user_joinModel = new T_User_joinModel();
        t_user_joinModel.setUser_id(req.getUser_id());
        t_user_joinModel.setPid(req.getPid());
        t_user_joinModel.setType_id(ProjectJoinEnum.CB.value());

        int check_count = t_project_tag_relationMapper.count(t_project_tag_relationModel);
        if (check_count >= 1) {
            //删除声音
            t_project_tag_relationMapper.delete(t_project_tag_relationModel);
            //减声音支持数
            t_project_tag_relationMapper.updateReduceTagCounter(req.getTid());
            //取消用户参标
            t_user_joinMapper.delete(t_user_joinModel);
            return js;
        }
        //update counter
        t_project_tag_relationMapper.updateTagCounter(req.getTid());
        //insert t_project_tag_relation日志
        t_project_tag_relationMapper.insert(t_project_tag_relationModel);
        //insert t_user_join 参标
        t_user_joinMapper.insert(t_user_joinModel);
        //参与打标积分
        t_integral_recordService.threadInsertIntegralRecord(
                IntegralRecordEnum.CYDBICO, req.getPid(), req.getUser_id());
        return js;
    }
}